﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace Alex.Library.Log
{
    /// <summary>
    /// 邮件存储日志
    /// </summary>
    public class SmtpLogger : LoggerBase
    {
        public SmtpLogger()
        {
            this._To = new List<string>();
            this._CC = new List<string>();
            this.Priority = MailPriority.Normal;
            this.From = new MailFrom(String.Empty, String.Empty, 25);
        }

        #region Properies

        private List<string> _To;
        /// <summary>
        /// 收件人地址列表
        /// </summary>
        public List<string> To
        {
            get { return this._To; }
        }

        private List<string> _CC;
        /// <summary>
        /// 抄送地址列表
        /// </summary>
        public List<string> CC
        {
            get { return this._CC; }
        }

        /// <summary>
        /// 邮件凭证
        /// </summary>
        public MailCredential Credential { get; set; }

        /// <summary>
        /// 邮件标题
        /// </summary>
        public string Subject { get; set; }

        /// <summary>
        /// 邮件内容是否是HTML
        /// </summary>
        public bool IsBodyHtml { get; set; }

        /// <summary>
        /// 邮件优先级
        /// </summary>
        public MailPriority Priority { get; set; }

        /// <summary>
        /// 发送方
        /// </summary>
        public MailFrom From { get; set; }

        #endregion

        /// <summary>
        /// 将日志发送到邮件
        /// </summary>
        /// <param name="level">日志级别</param>
        /// <param name="message">消息</param>
        protected override void DoFlush(LogLevel level, string message)
        {
            try
            {
                MailMessage mailMsg = new MailMessage();
                this.To.ForEach(address => mailMsg.To.Add(address));
                this.CC.ForEach(address => mailMsg.CC.Add(address));
                mailMsg.From = new MailAddress(this.From.Address, this.From.Address, Encoding.UTF8);
                mailMsg.Subject = this.Subject;
                mailMsg.SubjectEncoding = Encoding.UTF8;
                mailMsg.Body = this.Template.Format(level, message);
                mailMsg.BodyEncoding = Encoding.UTF8;
                mailMsg.IsBodyHtml = this.IsBodyHtml;
                mailMsg.Priority = this.Priority;

                SmtpClient client = new SmtpClient();
                client.Host = this.From.Host;
                client.Port = this.From.Port;
                client.UseDefaultCredentials = false;
                client.Credentials = new NetworkCredential(this.Credential.UserName, this.Credential.Password);
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                bool userToken = false;
                client.SendAsync(mailMsg, userToken);
            }
            catch (Exception ex)
            {
                //TODO
            }
        }
    }

    /// <summary>
    /// 发送方
    /// </summary>
    public struct MailFrom
    {
        public MailFrom(string address, string host, int port)
        {
            this.Address = address;
            this.Host = host;
            this.Port = port;
        }
        /// <summary>
        /// 发送方地址
        /// </summary>
        public string Address;
        /// <summary>
        /// 邮件服务器地址,如smtp.sohu.com
        /// </summary>
        public string Host;

        /// <summary>
        /// 发送端口,默认为25
        /// </summary>
        public int Port;
    }

    /// <summary>
    /// 邮件凭证
    /// </summary>
    public struct MailCredential
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        public MailCredential(string userName, string password)
        {
            this.UserName = userName;
            this.Password = password;
        }

        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName;

        /// <summary>
        /// 密码
        /// </summary>
        public string Password;
    }
}
